﻿using Microsoft.Practices.Unity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using Ymca.Web.Models.Account;
using Ymca.Web.Resources;
using Ymca.Web.Services.Interfaces;

namespace Ymca.Web.Controllers
{
	[Authorize]
	public class AccountController : Controller
	{
		private IUnityContainer _container;
		private IUserProfileService _userService;

		public AccountController()
		{
			_container = ContainerConfig.Container;
			_userService = _container.Resolve<IUserProfileService>();
		}

		[HttpGet]
		[AllowAnonymous]
		public ActionResult Login(string returnUrl)
		{
			ViewBag.ReturnUrl = returnUrl;
			return View();
		}

		[HttpPost]
		[AllowAnonymous]
		[ValidateAntiForgeryToken]
		public ActionResult Login(LoginViewModel viewModel, string returnUrl)
		{
			if (ModelState.IsValid)
			{
				if (Membership.ValidateUser(viewModel.Username, viewModel.Password))
				{
					MembershipUser user = Membership.GetUser(viewModel.Username);
					FormsAuthentication.SetAuthCookie(user.UserName, createPersistentCookie: viewModel.RememberMe);
					if (returnUrl != null)
					{
						return Redirect(returnUrl);
					}
					return RedirectToAction("Index", "Home");
				}
				ModelState.AddModelError(string.Empty, Messages.AuthenicationErrorMessage);
			}
			return View(viewModel);
		}

		[HttpPost]
		[ValidateAntiForgeryToken]
		public ActionResult LogOff()
		{
			FormsAuthentication.SignOut();
			return RedirectToAction("Index", "Home");
		}

		[HttpGet]
		[AllowAnonymous]
		public ActionResult Register(string returnUrl)
		{
			return View();
		}

		[HttpPost]
		[AllowAnonymous]
		[ValidateAntiForgeryToken]
		public ActionResult Register(RegisterViewModel viewModel, string returnUrl)
		{
			if (ModelState.IsValid)
			{
				MembershipUser user = Membership.CreateUser(viewModel.Username, viewModel.Password, viewModel.Email);
				if (user != null)
				{
					FormsAuthentication.SetAuthCookie(user.UserName, createPersistentCookie: false);
					_userService.AddEmptyProfile(user.UserName, user.Email);
					//TODO: Show Notification saying user, that he is able to edit his own profile.
					if (returnUrl != null)
					{
						return Redirect(returnUrl);
					}
					return RedirectToAction("Index", "Home");
				}
				ModelState.AddModelError(string.Empty, Messages.RegistrationErrorMessage);
			}
			return View(viewModel);
		}

		[HttpGet]
		public ActionResult ChangePassword()
		{
			return View();
		}

		[HttpPost]
		[ValidateAntiForgeryToken]
		public ActionResult ChangePassword(ChangePasswordViewModel viewModel)
		{
			if (ModelState.IsValid)
			{
				string username = User.Identity.Name;
				if (Membership.GetUser(username).ChangePassword(viewModel.OldPassword, viewModel.Password))
				{
					return RedirectToAction("Index");
				}
				ModelState.AddModelError(string.Empty, "Invalid old password");
				return View(viewModel);
			}
			return View(viewModel);
		}
	}
}
